﻿using KaroglanLauncher.Module;
using KaroglanSharedLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using WK.Libraries.BetterFolderBrowserNS;

namespace KaroglanLauncher
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public Dictionary<string, ModInfoWidget> widgetDict = new Dictionary<string, ModInfoWidget>();
        public Dictionary<string, List<ModInfoWidget>> categoryDict = new Dictionary<string, List<ModInfoWidget>>();
        public string currentCategory = "";
        public Config config;

        public MainWindow()
        {
            InitializeComponent();
            CheckWorkSpace();
            InitModWidget();
        }

        private void CheckWorkSpace()
        {
            config = Config.Load();
            Utils.BaseWorkSpace = config.workSpace;
            if (!Utils.IsLocationValid())
            {
                using (BetterFolderBrowser browser = new BetterFolderBrowser())
                {
                    browser.Title = "请选择工作区";
                    if (browser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        config.workSpace = browser.SelectedPath;
                        config.Save();
                        CheckWorkSpace();
                    }
                    else
                    {
                        MessageBox.Show("无法识别的工作目录，启动失败。");
                        Close();
                    }
                }
            }
        }

        private void InitModWidget()
        {
            if (Utils.IsLocationValid()) ModManager.UpdateModDict();
            else return;
            categoryDict = new Dictionary<string, List<ModInfoWidget>>();
            ClearElement();
            var btn = new Button();
            btn.Content = "全部";
            btn.Click += SwitchCategory;
            Category.Children.Add(btn);
            widgetDict = new Dictionary<string, ModInfoWidget>();
            var modict = ModManager.GetData();
            foreach( var modID in modict.Keys )
            {
                var modInfo = modict[modID];
                if (modInfo.isCoreMod) continue;
                AddElement(modInfo);
            }
        }

        private void SwitchCategory(object sender, System.Windows.RoutedEventArgs e)
        {
            var btn = (Button)sender;
            if (btn != null)
            {
                ClearElement();
                var categorys = new List<string>();
                if (btn.Content == "全部") categorys = categoryDict.Keys.ToList();
                else categorys.Add(btn.Content.ToString());

                foreach (var category in categorys)
                {
                    var mods = ModManager.GetData().Values;
                    foreach (var mod in mods.Where(info=> info.type == category))
                    {
                        AddElement(mod);
                    }
                }
            }
        }

        private void ClearElement()
        {
            ModListPanel.Children.Clear();
            widgetDict = new Dictionary<string, ModInfoWidget>();
        }

        private void AddElement(ModObj modObj)
        {
            var ElementWidget = new ModInfoWidget(modObj, this);
            ModListPanel.Children.Add(ElementWidget);
            widgetDict[modObj.modID] = ElementWidget;

            if (categoryDict.Keys.Contains(modObj.type)) categoryDict[modObj.type].Add(ElementWidget);
            else
            {
                categoryDict[modObj.type] = new List<ModInfoWidget> { ElementWidget };
                var btn = new Button();
                btn.Content = modObj.type;
                btn.Click += SwitchCategory;
                Category.Children.Add(btn);
            }
        }

        private void Launch_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Launcher.LaunchGame();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            //var update = new Updater(config.version);
            //update.Patch(@"D:\TempData\K14Client\output\14.4.1.zip", false);
            WebClient wc = new WebClient();
            string localPath = @"D:\Workspace\14.4.0.zip";
            wc.DownloadFile(@"https://gitee.com/Tollainmear/karoglan-database/blob/master/14.4.0/14.4.0.zip", localPath);
        }
    }
}